/***************************************************************************************************
  Copyright(C)   2011-8-24
  All rights reserved.
  File Name: database.h

  Description:数据底层操作模块
              数据最底层的一些操作,查找,删除代换

  ***************************************************************************************************/


#ifndef DATABASE_H
#define DATABASE_H

#include <QObject>
#include <QtSql>
#include <QSqlDatabase>
#include <QStringList>

#include "environment.h"


class DataBase
{
public:
    explicit DataBase(QObject *parent = 0);

    bool openDB(const QString& DBPath);          //打开数据库
    void setResourcePath(const QString& resource);

    bool closeDB();                              //关闭数据库
    bool createDB() const;                       //通过sql脚本，创建相应的数据表


    /****************************数据库取数据接口**********************************/


    int getRouteID(const QString& TISRouteCode, int trainDirectionID) const;
    int getStationID(const QString& TISStationCode, const int routeID) const;
    int getTrainStatusID(const QString& trainDirectionDes) const;
    int getTrainDirectID(const QString& TISRouteCode) const;
    int getPositionInCarID(const QString& TISRouteCode) const;

    //得到自定义包ID和默认包ID
    bool getPkgCustomPkgID(MainRelatIDStruct& mainRelatIDStruct) const;
    bool getPkgDefaultPkgID(MainRelatIDStruct& mainRelatIDStruct) const;

    LayoutStruct getLayoutStruct(const int layoutID, const int partitionID) const;

    HoliDesStruct getHoliDesStruct(const int holidayContentID) const;
    HoliPlayModStruct getHoliPlayModStruct(const int playModeID) const;
    HoliDesModStruct getHoliDesModStruct(const int holidayContentID) const;

    UsrCntDesStruct getUsrCntDesStruct(const int contentID) const;
    UsrCntTypeStruct getUsrCntTypeStruct(const int cntTypeID) const;

    QList<ContentStruct> getUsrCntStructList(const int contentID) const;
    QList<UsrInfoStru> getUsrInfoStruList(const int contentID) const;

    QList<PkgRelatStruct> getPkgRelatStructList(const int packageID) const;

    QList<ContentStruct> getHoliCntStuctList(const int contentID, const QString &holiday) const;
    QList<HoliInfoStruct> getHoliInfoStructList(const int contentID, const QString &holiday) const;

    /****************************数据库添加数据接口**********************************************/

    /****   代换或更新一条数据接口    ****/

    bool replaceLineStationTable(const StationStruct& stationStruct) const;
    bool replaceLineRouteTable(const RouteStruct& routeStruct) const;
    bool replaceLineTrainTable(const TrainStatusStruct& trainStatusStruct) const;
    bool replaceLinePositionInCarTable(const PosInCarStruct& posIncarStruct) const;
    bool replaceLineMainRelatTable(const MainRelatIDStruct& mainRelatIDStruct) const;
    bool replaceLinePkgRelationTable(const PkgRelatStruct& pkgRelatStruct) const;
    bool replaceLinePkgDesTable(const PkgDesStruct& pkgDesStruct) const;
    bool replaceLineLayoutTable(const LayoutStruct& layoutStruct) const;
    bool replaceLineHoliCntRelatTable(const ContentStruct& holidayCntStruct) const;
    bool replaceLineHoliCntDesTable(const HoliDesStruct& holiDesStruct) const;
    bool replaceLinePlayModeTable(const HoliPlayModStruct& holiPlayModStruct) const;
    bool replaceLineUsrCntRelatTable(const ContentStruct& usrContentStruct) const;
    bool replaceLineUsrCntDesTable(const UsrCntDesStruct& usrCntDesStruct) const;
    bool replaceLineUsrCntTypeTable(const UsrCntTypeStruct& usrCntTypestruct) const;


    /****   代换或更新多条数据接口    ****/

    bool replaceListStationTable(const QList<StationStruct>& stationStructList) const;
    bool replaceListRouteTable(const QList<RouteStruct>& routeStructList) const;
    bool replaceListTrainTable(const QList<TrainStatusStruct>& trainStatusStructList) const;
    bool replaceListPositionInCarTable(const QList<PosInCarStruct>& posIncarStructList) const;
    bool replaceListMainRelatTable(const QList<MainRelatIDStruct>& mainRelatIDStructList) const;
    bool replaceListPkgRelationTable(const QList<PkgRelatStruct>& pkgRelatStructList);
    bool replaceListPkgDesTable(const QList<PkgDesStruct>& pkgDesStructList) const;
    bool replaceListLayoutTable(const QList<LayoutStruct>& layoutStructList)  const;
    bool replaceListHoliCntTable(const QList<HoliDesStruct>& holiDesStructList) const;
    bool replaceListHoliCntDesRelatTable(const QList<ContentStruct>& holidayCntStructList) const;
    bool replaceListplayModeTable(const QList<HoliPlayModStruct>& holiPlayModStructList) const;
    bool replaceListUsrCntRelatTable(const QList<ContentStruct>& usrContStructList) const;
    bool replaceListUsrCntDesTable(const QList<UsrCntDesStruct>& usrCntDesStructList) const;
    bool replaceListUsrCntTypeTable(const QList<UsrCntTypeStruct>& usrCntTypestructList) const;



    /******* 删除数据接口 *******/
    bool delLinesStationTable(const int stationID) const;
    bool delLinesRouteTable(const int routeID) const;
    bool delLinesPosInCarTable(const int posInCarID) const;
//    bool delLinesTrainStatus(const TrainStatusStruct& trainStatusStruct);
    bool delLinesMainRelatTable(const MainRelatIDStruct& mainRelatIDStruct) const;
    bool delLinesPkgRelatTable(const int packageID) const;
    bool delLinesPkgDesTable(const int packageID) const;
    bool delLinesLayoutTable(const int layoutID, const int partitionID) const;
    bool delLinesHoliCntRelatTable(const int contentID, const QString& holiday) const;
    bool delLinesHoliCntDesTable(const int holidayContentID) const;
    bool delLinesPlayModeTable(const int playModeID) const;
    bool delLinesUsrCntRelatTable(const int contentID) const;
    bool delLinesUsrCntDesTable(const int contentID) const;
    bool delLinesUsrCntTypeTable(const int contentTypeID) const;


    bool deletePkgTabelData();

private:
    bool mConnect;               //connect 为数据库打开状态，如果为true,则末打开，可以进行打开操作，否则则是已经打开数据库了
    QSqlDatabase *mDB;           //数据库连接变量，通过此变量和相应的数据库进行连接
    QString resourcePath;

};

#endif // DATABASE_H
